package com.gpoint.rdp.door.area.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gpoint.rdp.base.core.util.string.StringUtil;
import com.gpoint.rdp.base.core.entity.ResultMessage;
import com.gpoint.rdp.base.web.controller.GenericController;
import com.gpoint.rdp.base.web.util.RequestUtil;
import com.gpoint.rdp.common.DoorConfig;
import com.gpoint.rdp.door.area.service.ZoneService;


import com.gpoint.rdp.door.area.service.ZoneQueryService;
import com.gpoint.rdp.door.area.persistence.dao.ZoneQueryDao;
import com.gpoint.rdp.door.area.persistence.entity.ZonePo;
import java.util.ArrayList;
import java.util.HashMap;

import com.gpoint.rdp.base.core.util.AppUtil;
import com.gpoint.rdp.base.core.util.BeanUtils;


import java.util.List;
import java.util.Map;

/**
 * TB_DOOR_ZONE 控制类
 *
 * <pre>
 *  
 * 开发公司：苏州东大金点信息技术有限公司
 * 开发人员：唐红锋
 * 邮箱地址：55872655@qq.com
 * 创建时间：2019-10-23 18:09:08
 * </pre>
 */
@Controller
@RequestMapping("/rdp/area/zone/")
public class ZoneController extends GenericController {
	@Resource
	private ZoneService zoneService;
	@Resource
	private ZoneQueryService zoneQueryService;
	
	

	
	
	DoorConfig config=AppUtil.getBean(DoorConfig.class);
    String servicecode=config.getServicecode();

	@RequestMapping("tree")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = getAutoView();
		return mv;
	}

	/**
	 * 获取结构数据
	 * 
	 * @param request
	 * @param reponse
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("getTreeData")
	public @ResponseBody List<ZonePo> getTreeData(HttpServletRequest request, HttpServletResponse reponse)
			throws Exception {
		try {
			String listData = zoneQueryService.findAll();

			List<ZonePo> zoneList = ZonePo.fromJsonArrayString(listData);

			if (BeanUtils.isEmpty(zoneList)) {
				zoneList = new ArrayList<ZonePo>();
			}
			/* 构建TB_DOOR_ZONE根节点 */
			ZonePo zone = new ZonePo();
			zone.setId("0");
			zone.setName("区域");
			zoneList.add(zone);
			return zoneList;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	}

	@RequestMapping("tree_for_edit")
	@ResponseBody
	public List<Map<String, Object>> listJsonByServiceCode(HttpServletRequest request, HttpServletResponse reponse)
			throws Exception {
		try {
			String listData = zoneQueryService.findAll();
			List<ZonePo> list = ZonePo.fromJsonArrayString(listData);
			List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
			Map<String, Object> root = new HashMap<String, Object>(100);
			root.put("parentId", "");
			root.put("id", 0);
			root.put("name","区域");
			root.put("key", 0);
		    listMap.add(root);
			if (!BeanUtils.isEmpty(list)) {				
				for (ZonePo po : list) {
					Map<String, Object> map = new HashMap<String, Object>(256);
					map.put("parentId", po.getPid());
					map.put("id", po.getId());
					map.put("name", po.getName());
					map.put("key", po.getId());
					listMap.add(map);
				}
			}
			return listMap;
		} catch (Exception e) {
			return null;
		}
	}
	
	
	@RequestMapping("list_json_tree")
	@ResponseBody
	public List<Map<String, Object>> listJsonForSelect(HttpServletRequest request, HttpServletResponse reponse)
			throws Exception {
		try {
			String listData = zoneQueryService.findAll();
			List<ZonePo> list = ZonePo.fromJsonArrayString(listData);
			if (!BeanUtils.isEmpty(list)) {
				List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
				for (ZonePo po : list) {
					Map<String, Object> map = new HashMap<String, Object>(256);
					map.put("parentId", po.getPid());
					map.put("id", po.getId());
					map.put("name", po.getName());
					map.put("key", po.getId());
					listMap.add(map);
				}
				return listMap;
			}
			return null;
		} catch (Exception e) {
			return null;
		}
	}

	
	
	@RequestMapping("list_json_tree_zone")
	@ResponseBody
	public List<Map<String, Object>> listJsonForSelect1(HttpServletRequest request, HttpServletResponse reponse)
			throws Exception {
		try {
			List<String> paramList=new ArrayList<String>();
			  //0,8,9,13,10
//			 String a="0,8,9,13,10";
			 String a= RequestUtil.getString(request, "ids");
			 
			   String[] arr = a.split(",");
			   for (int i = 0; i < arr.length; i++) {
				paramList.add(arr[i]);
			  }
			List<ZonePo> list2 = this.zoneQueryService.findByIds(paramList);
			   
			if (!BeanUtils.isEmpty(list2)) {
				List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
				for (ZonePo po : list2) {
					Map<String, Object> map = new HashMap<String, Object>(256);
					map.put("parentId", po.getPid());
					map.put("id", po.getId());
					map.put("name", po.getName());
					map.put("key", po.getId());
					listMap.add(map);
				}
				return listMap;
			}
			return null;
		} catch (Exception e) {
			return null;
		}
	}
	
	
	/**
	 * 编辑【TB_DOOR_ZONE】信息页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("edit")
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String preUrl = RequestUtil.getPrePage(request);
		String id = RequestUtil.getString(request, "id");
		String pid = RequestUtil.getString(request, "pid");
		String parentFullName = getNodeFullName(pid);;
		ZonePo zone = null;
		if (StringUtil.isNotEmpty(id)) {
			String data = null;
			data = zoneQueryService.get(id);
			zone = ZonePo.fromJsonString(data);
			zone.setParentFullName(parentFullName);
		} else {
			zone = new ZonePo();
			zone.setPid(pid);
			zone.setParentFullName(parentFullName);
			zone.setServicecode(servicecode);
		}
		
		return getAutoView().addObject("zone", zone).addObject("returnUrl", preUrl);
	}

	/**
	 * 编辑【TB_DOOR_ZONE】信息页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("flowEdit")
	public ModelAndView flowEdit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String preUrl = RequestUtil.getPrePage(request);
		String id = RequestUtil.getString(request, "id");
		ZonePo zone = null;
		if (StringUtil.isNotEmpty(id)) {
			String data = null;
			data = zoneQueryService.get(id);
			zone = ZonePo.fromJsonString(data);
		}
		return getAutoView().addObject("zone", zone).addObject("returnUrl", preUrl);
	}

	/**
	 * 【TB_DOOR_ZONE】明细页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("get")
	public ModelAndView get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String preUrl = RequestUtil.getPrePage(request);
		String id = RequestUtil.getString(request, "id");
		ZonePo zone = null;
		if (StringUtil.isNotEmpty(id)) {
			String data = null;
			data = zoneQueryService.get(id);
			zone = ZonePo.fromJsonString(data);
		}
		return getAutoView().addObject("zone", zone).addObject("returnUrl", preUrl);
	}

	/**
	 * 保存【TB_DOOR_ZONE】信息
	 *
	 * @param request
	 * @param response
	 * @param zone
	 * @throws Exception
	 */
	@RequestMapping("save")
	public void save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultMessage message = null;
		try {
			String json = RequestUtil.getString(request, "json");
			zoneService.save(json);
			message = new ResultMessage(ResultMessage.SUCCESS, "保存成功");
		} catch (Exception e) {
			message = new ResultMessage(ResultMessage.FAIL, "操作失败," + e.getMessage());
			logger.error("对TB_DOOR_ZONE操作失败，" + e.getMessage(), e);
		}
		writeResultMessage(response.getWriter(), message);
	}

	/**
	 * 批量删除【TB_DOOR_ZONE】记录
	 *
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("remove")
	public void remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultMessage message = null;
		try {
			// 获得待删除的id
			String[] ids = RequestUtil.getStringAryByStr(request, "id");
			zoneService.deleteByIds(ids);
			message = new ResultMessage(ResultMessage.SUCCESS, "删除成功");
		} catch (Exception e) {
			message = new ResultMessage(ResultMessage.FAIL, "删除失败，" + e.getMessage());
			logger.error("删除TB_DOOR_ZONE失败，" + e.getMessage(), e);
		}
		writeResultMessage(response.getWriter(), message);
	}

	private String getNodeFullName(String pid) {
		String name = "";
		while (StringUtils.isNotEmpty(pid) && !"0".equals(pid)) {
			String data = zoneQueryService.get(pid);
			ZonePo zone = ZonePo.fromJsonString(data);
			if (BeanUtils.isNotEmpty(zone)) {
				pid = zone.getPid();
				name = StringUtils.isEmpty(name)?zone.getName():zone.getName() + "/" + name;
			} else {
				pid = "";
			}
		}
		return name;
	}

}